﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Converstion
/// </summary>
public class Convertion
{
	public Convertion()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static List<GProduct> ColorFill(List<GProduct> list)
    {
        if (list != null && list.Count > 0)
        {
            foreach (GProduct gproduct in list)
            {
                string nColor = "";
                while (true)
                {
                    nColor = Convertion.generateColor();
                    if (!checkColorExist(list, nColor))
                    {
                        break;
                    }
                }
                gproduct.ProductColor = nColor;
            }
        }
        return list;
    }

    private static string generateColor()
    {
        String[] arrRange = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D" , "E", "F" };
        string scolor = "#";
        Random rand = new Random();
        for (int i = 0; i < 6; i++)
        {
            int iPos = rand.Next(arrRange.Length);
            scolor += arrRange[iPos];
        }
        return scolor;
    }

    private static bool checkColorExist(List<GProduct> list, string color)
    {

        foreach (GProduct gproduct in list)
        {
            if (gproduct.ProductColor == color)
            {
                return true;
            }
        }

        return false;
    }
}